<?php
	class Db_customer extends CI_Model{
		public function __construct(){
			parent::__construct();
		}
		
		public function listall(){
			$config['base_url'] = base_url().'manage_customer/customer_list';
			$config['total_rows'] = $this->db->count_all("mobile_customer");
			$config['per_page'] = 20;
			$config['full_tag_open'] = '<div class="pages">';
			$config['full_tag_close'] = '<div>';
			$this->pagination->initialize($config);
			//$this->db->limit($config['per_page'],$this->uri->segment(3));
			$this->db->order_by("pkid", "desc");
			$res = $this->db->get('mobile_customer');
			//==
			$no = $this->uri->segment(3)+1;
			$i = 1;
			$arr = $res->result_array();
			$cat = array();
			foreach ($arr as $P){
				if($i %2 == 0)
					$tmp['back']							="#FFFFFF";
				else
					$tmp['back']							="#F4F4F4";
				$i++;
				$tmp['no']							=$no;
				$tmp['pkid']						=$P['pkid'];
				$tmp['name']						=$P['name'];
				$tmp['email']						=$P['email'];
				$cat[] = $tmp;
				$no = $no+1;
			}
			$num_row = $config['total_rows'];
			$pagelink  = $this->pagination->create_links();
			return array($cat,$num_row,$pagelink,$config['total_rows']);
		}
		
		public function customer_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("category","Room Category","required");
				$this->form_validation->set_rules("room_no","Room No.","required");
				$this->form_validation->set_rules("name","Name Surname","required");
				$this->form_validation->set_rules("startdate","Check In","required");
				$this->form_validation->set_rules("enddate","Check Out","required");
				$this->form_validation->set_rules("phone","phone","trim");
				$this->form_validation->set_rules("fax","fax","trim");
				$this->form_validation->set_rules("email","email","trim|valid_email");
				$this->form_validation->set_rules("welcome","welcome","trim");
				$this->form_validation->set_rules("address1","address1","trim");
				$this->form_validation->set_rules("address2","address2","trim");
				$this->form_validation->set_rules("country","country","trim");
				$this->form_validation->set_rules("point","point","trim|numeric");
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name",set_value('name'));
					$this->cismarty->assign("phone",set_value('phone'));
					$this->cismarty->assign("fax",set_value('fax'));
					$this->cismarty->assign("email",set_value('email'));
					$this->cismarty->assign("welcome",set_value('welcome'));
					$this->cismarty->assign("address1",set_value('address1'));
					$this->cismarty->assign("address2",set_value('address2'));
					$this->cismarty->assign("point",set_value('point'));
					$this->cismarty->assign("startdate",set_value('startdate'));
					$this->cismarty->assign("enddate",set_value('enddate'));
						
					//set error
					$this->cismarty->assign("errorname",form_error('name'));
					$this->cismarty->assign("erroremail",form_error('email'));
					$this->cismarty->assign("errorpoint",form_error('point'));
					$this->cismarty->assign("errorcate",form_error('category'));
					$this->cismarty->assign("errorroom",form_error('room_no'));
					$this->cismarty->assign("errorstartdate",form_error('startdate'));
					$this->cismarty->assign("errorenddate",form_error('enddate'));
				}else {
					$data = array(
							"name"=>trim($this->input->post('name')),
							"phone"=>trim($this->input->post('phone')),
							"fax"=>trim($this->input->post('fax')),
							"email"=>trim($this->input->post('email')),
							"txt_welcome"=>addslashes($this->input->post('welcome')),
							"address1"=>trim($this->input->post('address1')),
							"address2"=>trim($this->input->post('address2')),
							"country_pkid"=>trim($this->input->post('country')),
							"point"=>trim($this->input->post('point')),
							"created_by"=>$this->session->userdata("sess_username"),
							"created_date"=>date("Y-m-d H:i:s")
					);
					$res = $this->db->insert('hotel_customer',$data);
					$last_id = $this->db->insert_id();
					$arr_tran = array(
							"customer_pkid"=>$last_id,
							"room_pkid"=>$this->input->post('room_no'),
							"check_in_date"=>$this->input->post('startdate'),
							"check_out_date"=>$this->input->post('enddate'),
							"created_date"=>date("Y-m-d H:i:s")
					);
					$this->db->insert("hotel_customer_transaction",$arr_tran);
					if($res) redirect('manage_customer/customer_list/success');
					else  redirect('manage_customer/customer_list/fail');
				}
		
			}
			$this->load->model("apanel/db_hotel");
			$this->cismarty->assign("tiny",$this->template->tinymce());
			$this->cismarty->assign("str_country",$this->db_hotel->country_list());
			$this->load->model("apanel/db_room");
			$this->cismarty->assign("str_cate",$this->db_room->show_cat());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(7));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open('manage_customer/customer_add'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/customer/customer_add.html');
		}
		
		public function customer_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("category","Room Category","required");
				$this->form_validation->set_rules("room_no","Room No.","required");
				$this->form_validation->set_rules("startdate","Check In","required");
				$this->form_validation->set_rules("enddate","Check Out","required");
				$this->form_validation->set_rules("name","Name Surname","required");
				$this->form_validation->set_rules("phone","phone","trim");
				$this->form_validation->set_rules("fax","fax","trim");
				$this->form_validation->set_rules("email","email","trim|valid_email");
				$this->form_validation->set_rules("welcome","welcome","trim");
				$this->form_validation->set_rules("address1","address1","trim");
				$this->form_validation->set_rules("address2","address2","trim");
				$this->form_validation->set_rules("country","country","trim");
				$this->form_validation->set_rules("point","point","trim|numeric");
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name",set_value('name'));
					$this->cismarty->assign("phone",set_value('phone'));
					$this->cismarty->assign("fax",set_value('fax'));
					$this->cismarty->assign("email",set_value('email'));
					$this->cismarty->assign("welcome",set_value('welcome'));
					$this->cismarty->assign("address1",set_value('address1'));
					$this->cismarty->assign("address2",set_value('address2'));
					$this->cismarty->assign("point",set_value('point'));
					$this->cismarty->assign("startdate",set_value('startdate'));
					$this->cismarty->assign("enddate",set_value('enddate'));
		
					//set error
					$this->cismarty->assign("errorname",form_error('name'));
					$this->cismarty->assign("erroremail",form_error('email'));
					$this->cismarty->assign("errorpoint",form_error('point'));
					$this->cismarty->assign("errorcate",form_error('category'));
					$this->cismarty->assign("errorroom",form_error('room_no'));
					$this->cismarty->assign("errorstartdate",form_error('startdate'));
					$this->cismarty->assign("errorenddate",form_error('enddate'));
				}else {
					$data = array(
							"name"=>trim($this->input->post('name')),
							"phone"=>trim($this->input->post('phone')),
							"fax"=>trim($this->input->post('fax')),
							"email"=>trim($this->input->post('email')),
							"txt_welcome"=>addslashes($this->input->post('welcome')),
							"address1"=>trim($this->input->post('address1')),
							"address2"=>trim($this->input->post('address2')),
							"country_pkid"=>trim($this->input->post('country')),
							"point"=>trim($this->input->post('point')),
							"created_by"=>$this->session->userdata("sess_username"),
							"created_date"=>date("Y-m-d H:i:s")
					);
					$this->db->where("pkid",$id);
					$res = $this->db->update('hotel_customer',$data);
					//== check in
					$arr_chekin = array(
							"room_pkid"=>$this->input->post('room_no'),
							"check_in_date"=>$this->input->post('startdate'),
							"check_out_date"=>$this->input->post('enddate')
					);
					$this->db->update("hotel_customer_transaction",$arr_chekin);
						
					if($res) redirect('manage_customer/customer_list/success');
					else  redirect('manage_customer/customer_list/fail');
				}
					
			}else {
				$this->db->where("pkid",$id);
				$res = $this->db->get('hotel_customer');
				$row = $res->row();
				$this->cismarty->assign("name",$row->name);
				$this->cismarty->assign("phone",$row->phone);
				$this->cismarty->assign("fax",$row->fax);
				$this->cismarty->assign("email",$row->email);
				$this->cismarty->assign("welcome",stripslashes($row->txt_welcome));
				$this->cismarty->assign("address1",$row->address1);
				$this->cismarty->assign("address2",$row->address2);
				$this->cismarty->assign("point",$row->point);
				$country = $row->country_pkid;
				$this->db->where("customer_pkid",$id);
				$rowcate = $this->db->get('hotel_customer_transaction')->row();
				$room_id = $room_no = $rowcate->room_pkid;
				$this->cismarty->assign("startdate",$rowcate->check_in_date);
				$this->cismarty->assign("enddate",$rowcate->check_out_date);
		
			}
			$this->load->model("apanel/db_hotel");
			$this->cismarty->assign("tiny",$this->template->tinymce());
			$this->cismarty->assign("str_country",$this->db_hotel->country_list($country));
			$this->load->model("apanel/db_room");
			list($group,$str_room) =$this->db_customer->room_checkin($room_id);
			$this->cismarty->assign("str_cate",$this->db_room->show_cat($group));
			$this->cismarty->assign("str_room",$str_room);
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(7));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_customer/customer_edit/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/customer/customer_edit.html');
		}
		
		public function customer_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_customer");
				if($res) redirect('manage_customer/customer_list/success');
			}
		}
		
		public function cutomer_delAll(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("mobile_customer");
				}
			}
			redirect('manage_customer/customer_list/success');
		}
	}
?>